package at.ac.univie.zsu.aguataplan.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.property.ShortcutProperties;
import at.ac.univie.zsu.aguataplan.property.UIProperties;

/**
 * @author gerry
 * 
 */
public class VideoControlPanel extends JPanel implements UIConstants {
	private static final long serialVersionUID = 3985611336305503255L;

	private static final int LOG_UP_INTERVAL = 100;
	private static final int LOG_UP_INT = 1000;

	private IVideoPlayer videoPlayer;

	private GeneralProperties generalProperties;
	private ShortcutProperties shortcutProperties;
	private UIProperties guiProperties;
	private I18nProperties m;

	private JButton eventButton;
	private JButton playButton;
	private JButton stopButton;
	private JButton pauseButton;
	private JButton increaseSpeedButton;
	private JButton decreaseSpeedButton;
	private JButton fullscreenButton;

	private JSlider volumeSlider;

	private JLabel speedLabel;
	private JLabel positionLabel;
	private JLabel lengthLabel;

	private JSlider positionSlider;

	private boolean pressed = false;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public VideoControlPanel(GeneralProperties generalProperties, IVideoPlayer videoPlayer) {
		super();
		this.generalProperties = generalProperties;
		this.videoPlayer = videoPlayer;

		shortcutProperties = generalProperties.getShortcutProperties();
		guiProperties = generalProperties.getUiProperties();
		m = generalProperties.getI18nProperties();

		volumeSlider = new JSlider(VOLUME_MIN_SLIDER, VOLUME_MAX_SLIDER, guiProperties.getVolumeStart());

		playButton = createButton(shortcutProperties.getScPlay(), ICON_PLAY);
		stopButton = createButton(shortcutProperties.getScStop(), ICON_STOP);
		pauseButton = createButton(shortcutProperties.getScPause(), ICON_PAUSE);
		increaseSpeedButton = createButton(shortcutProperties.getScIncreaseSpeed(), ICON_INCREASESPEED);
		decreaseSpeedButton = createButton(shortcutProperties.getScDecreaseSpeed(), ICON_DECREASESPEED);
		fullscreenButton = createButton(shortcutProperties.getScFullscreen(), ICON_FULLSCREEN);

		speedLabel = new JLabel(videoPlayer.getPlaybackSpeed());
		positionLabel = new JLabel(videoPlayer.getPlaybackPosition());
		lengthLabel = new JLabel(videoPlayer.getPlaybackPositionMaximum());

		positionSlider = new JSlider(POSITION_MIN_SLIDER, POSITION_MAX_SLIDER, POSITION_ACT_SLIDER);

		add(volumeSlider);
		add(playButton);
		add(stopButton);
		add(pauseButton);
		add(decreaseSpeedButton);
		add(increaseSpeedButton);
		add(fullscreenButton);
		add(speedLabel);
		add(positionLabel);
		add(lengthLabel);
		add(positionSlider);

		// ActionListener
		playButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				VideoControlPanel.this.videoPlayer.play();
				speedLabel.setText(VideoControlPanel.this.videoPlayer.getPlaybackSpeed());

			}
		});

		stopButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				VideoControlPanel.this.videoPlayer.stop();
				positionSlider.setValue(POSITION_MIN_SLIDER);
			}
		});

		pauseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				VideoControlPanel.this.videoPlayer.pause();
			}
		});

		decreaseSpeedButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				VideoControlPanel.this.videoPlayer.decreaseSpeed();
				speedLabel.setText(VideoControlPanel.this.videoPlayer.getPlaybackSpeed());
			}
		});

		increaseSpeedButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				VideoControlPanel.this.videoPlayer.increaseSpeed();
				speedLabel.setText(VideoControlPanel.this.videoPlayer.getPlaybackSpeed());
			}
		});

		fullscreenButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				VideoControlPanel.this.videoPlayer.fullscreen();

			}
		});

		volumeSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JSlider s = (JSlider) e.getSource();
				VideoControlPanel.this.videoPlayer.setVolume(s.getValue());

			}
		});

		positionSlider.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				JSlider s = (JSlider) e.getSource();
				VideoControlPanel.this.videoPlayer.setPosition(s.getValue());
				pressed = false;
			}

			@Override
			public void mousePressed(MouseEvent e) {
				pressed = true;
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});

		ActionListener taskPerformer = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				positionLabel.setText(VideoControlPanel.this.videoPlayer.getPlaybackPosition());
				// TODO: would not be necessary to renew text every time
				lengthLabel.setText(VideoControlPanel.this.videoPlayer.getPlaybackPositionMaximum());

			}
		};
		Timer t = new Timer(LOG_UP_INTERVAL, taskPerformer);
		t.start();

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public void changePositionSlider(float position) {
		float p = position * POSITION_MAX_SLIDER;
		if (!pressed) {
			positionSlider.setValue((int) p);
		}

	}

	private JButton createButton(String shortcut, String icon) {
		JButton button;
		button = new JButton(new ImageIcon(guiProperties.getIconPath() + icon));
		shortcut = shortcut.substring(0, 1);
		shortcut = shortcut.toUpperCase().substring(0, 1);
		int ch = shortcut.charAt(0);
		InputMap inputMap = button.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
		KeyStroke keyButton = KeyStroke.getKeyStroke(ch, 0);
		inputMap.put(keyButton, ch);
		button.getActionMap().put(ch, new ButtonClickAction(button));
		return button;
	}

}
